package com.microservices.yucloud.appweb.controller;

import com.alibaba.fastjson.JSON;
import com.microservices.yucloud.appweb.annotation.CacheLock;
import com.microservices.yucloud.appweb.annotation.CacheParam;
import com.microservices.yucloud.appweb.disruptor.NotifyService;
import com.microservices.yucloud.appweb.disruptor.OrderEventProducer;
import com.microservices.yucloud.appweb.domain.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zy
 * @date 2019-08-28 19:09
 **/
@Slf4j
@RestController
@RequestMapping("test")
public class TestController {

    @Autowired
    private NotifyService notifyService;

    @Autowired
    private OrderEventProducer orderEventProducer;

    @GetMapping("/disruptor")
    public String testDisruptor() {
        notifyService.sendMessage("test-disruptor");
        return "ok";
    }

    @GetMapping("/orderEventProducer")
    public String testOrderEventProducer() {
        Orders orders = new Orders();
        orders.setId("1");
        orders.setState("1");
        orders.setUserId("123");
        orders.setAddress("aaa");
        orders.setPhoneNum("13311111111");
        for (int i = 0; i < 1000; i++) {
            orders.setId("" + i);
            orderEventProducer.send("test-order" + i, JSON.toJSONString(orders));
        }
        return "ok";
    }

    @CacheLock(prefix = "books")
    @GetMapping("/cache")
    public String query(@CacheParam(name = "token") @RequestParam String token) {
        return "success - " + token;
    }
}
